package servlet;

import hbase.HbaseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:xuyuanhong
 * @Date:Created in 2022/3/8 12:59
 */
@WebServlet(name = "loginServlet",urlPatterns ="/user")
public  class loginServlet extends HttpServlet {

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String tname = "admintable";
            String columnFamily ="ad_info";
            String uName="username";
            String userName = request.getParameter("userName");
            String userPassword = request.getParameter("userPassword");
            String msg = "-";
            try {
//                msg =HbaseUtil.scan(tname);
                msg = HbaseUtil.queryequal(tname, columnFamily,uName,userName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            int result1=msg.indexOf(userName);
            int result2=msg.indexOf(userPassword);//全字段匹配的相关代码？？？？？？
            if (result1>0&&result2>0)
            {
                response.setCharacterEncoding("utf-8");
                request.getRequestDispatcher("/supermarket.jsp").forward(request,response);
            }else{
                request.setAttribute("msg","用户名或者密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
//                response.setCharacterEncoding("utf-8");
//                response.setContentType("text/html;charset=utf-8");
//                PrintWriter out = response.getWriter();
//                out.println(msg+"hahahahahaha");
//                out.flush();
//                out.close();测试
            }

        }
    }



